package com.jiutong.meipengdai.ui.activitys;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jiutong.meipengdai.R;
import com.jiutong.meipengdai.base.BaseActivity;
import com.jiutong.meipengdai.entity.BaseRespond;
import com.jiutong.meipengdai.entity.IdentifyCodeRespond;
import com.jiutong.meipengdai.http.model.interfaces.RegisterAndLoginBiz;
import com.jiutong.meipengdai.http.model.requests.UserInfoRequest;
import com.jiutong.meipengdai.http.present.UserInfoPresent;
import com.jiutong.meipengdai.http.subscribers.ProgressSubscriber;
import com.jiutong.meipengdai.http.subscribers.SubscriberOnNextListener;
import com.jiutong.meipengdai.utils.TimerUtil;

import java.util.HashMap;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by suxi on 2017/6/3.
 */

public class EmailBinddingActivity extends BaseActivity implements RegisterAndLoginBiz {
    @Bind(R.id.rl_back)
    RelativeLayout rl_back;
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.et_email)
    EditText etEmail;
    @Bind(R.id.et_email_code)
    TextView et_email_code;
    @Bind(R.id.tv_email_getcode)
    TextView tv_email_getcode;
    @Bind(R.id.btn_email_ok)
    Button btnOk;

    private UserInfoPresent userInfoPresent;


    private TimerUtil timerUtil;

    @Override
    public int getLayoutResouceId() {
        return R.layout.activity_email_bindding;
    }

    @Override
    protected void initView() {
        super.initView();
        ButterKnife.bind(this);
        tvTitle.setText("绑定邮箱");

        btnOk.setEnabled(false);
        EditTextChange etc = new EditTextChange();
        etEmail.addTextChangedListener(etc);
        et_email_code.addTextChangedListener(etc);

        timerUtil = new TimerUtil(tv_email_getcode);
        timerUtil.setCountTimer();


        userInfoPresent = new UserInfoPresent(this, this);
    }

    class EditTextChange implements TextWatcher {

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            boolean isPwdOk = getEmail().length()>0;
            boolean isRePwdOk = getVerificatCode().length() == 6;
            if(isPwdOk&isRePwdOk){
                btnOk.setBackgroundResource(R.drawable.selector_orange_btn_bg);
                btnOk.setEnabled(true);
            }else{
                btnOk.setBackgroundResource(R.drawable.shape_light_gray_btn);
                btnOk.setEnabled(false);
            }
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    }

    @OnClick({R.id.rl_back, R.id.btn_email_ok, R.id.tv_email_getcode})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.rl_back:
                finish();
                break;
            case R.id.tv_email_getcode:
                if (isRightAction()){
                    userInfoPresent.emailVerify();
                }
                break;
            case R.id.btn_email_ok:
                if (isRightAction()) {
                    if (TextUtils.isEmpty(getVerificatCode())) {
                        toastShortShow("验证码不能为空");
                        return ;
                    }
                    SubscriberOnNextListener onNextListener = new SubscriberOnNextListener<BaseRespond>() {
                        @Override
                        public void onNext(BaseRespond subjects) {
                            BaseRespond respond = (BaseRespond)subjects;
                            if(respond.getResCode().equals("200")) {
                                toastShortShow(respond.getResDesc());
                                Bundle bundle = new Bundle();
                                bundle.putString("email", getEmail());
                                openActivity(AccountSettingActivity.class, bundle);
                                finish();
                            }else{
                                toastShortShow(respond.getResDesc());
                            }
                        }

                        @Override
                        public void onError(Throwable e) {
                        }
                    };

                    HashMap<String, Object> fieldsMap = new HashMap<>();
                    fieldsMap.put("email", getEmail());
                    fieldsMap.put("authCode", getVerificatCode());

                    new UserInfoRequest().emailBind(new ProgressSubscriber(onNextListener, mContext), fieldsMap);
                }
                break;
        }
    }

    /**
     * 判断是否正确
     *
     * @return
     */

    private boolean isRightAction() {
        if (TextUtils.isEmpty(getEmail())) {
            toastShortShow("邮箱不能为空");
            return false;
        }
//        if (!isEmail(getEmail())) {
//            toastShortShow("您输入的邮箱不合法");
//            return false;
//        }
        return true;
    }

    private boolean isEmail(String email) {
        String regex = "^[A-Za-z]{1,40}@[A-Za-z0-9]{1,40}\\.[A-Za-z]{2,3}$";
        return email.matches(regex);
    }

    @Override
    public String getPhoneNum() {
        return null;
    }

    @Override
    public String getPassword() {
        return null;
    }

    @Override
    public String getVerificatCode() {
        return et_email_code.getText().toString().trim();
    }

    @Override
    public String getOriPwd() {
        return null;
    }

    @Override
    public String getNewPwd() {
        return null;
    }

    @Override
    public String getRelName() {
        return null;
    }

    @Override
    public String getIdCard() {
        return null;
    }

    @Override
    public String getEmail() {
        return etEmail.getText().toString().trim();
    }

    @Override
    public String getNewPhone() {
        return null;
    }

    @Override
    public String getCardNumber() {
        return null;
    }

    @Override
    public void onVerCodeGetSuccess(IdentifyCodeRespond subjects) {

    }

    @Override
    public void onSuccess(Object object) {
        BaseRespond respond = (BaseRespond) object;
        if (respond.getResCode().equals("200")) {
            toastShortShow("邮箱验证码发送成功");
            timerUtil.startTimer();
        } else if (respond.getResCode().equals("400")) {
            toastShortShow("邮箱验证码发送失败");
        } else if (respond.getResCode().equals("401")) {
            toastShortShow("邮箱已被绑定");
        }
    }

    @Override
    public void onError(Throwable e) {

    }
}
